package file_rw;

//import java.io.BufferedWriter;
//import java.io.FileReader;
//import java.io.FileWriter;
import java.io.File;
import java.io.RandomAccessFile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileRW {
	
	public static int  byte2int(byte[] bytes,int start){
		 int num = bytes[3+start] & 0xFF;  
		    num |= ((bytes[2+start] << 8) & 0xFF00);  
		    num |= ((bytes[1+start] << 16) & 0xFF0000);  
		    num |= ((bytes[0+start] << 24) & 0xFF000000);  
		    return num;   
	}
	
	public static int  byte2int(byte[] bytes){
		int num = bytes[3] & 0xFF;  
		    num |= ((bytes[2] << 8) & 0xFF00);  
		    num |= ((bytes[1] << 16) & 0xFF0000);  
		    num |= ((bytes[0] << 24) & 0xFF000000);  
		    return num;   
	}
	
	public static byte[] int2byte(int i) {  
	    byte[] bt = new byte[4];  
	    bt[3] = (byte) (0xff & i);  
	    bt[2] = (byte) ((0xff00 & i) >> 8);  
	    bt[1] = (byte) ((0xff0000 & i) >> 16);  
	    bt[0] = (byte) ((0xff000000 & i) >> 24);  
	    return bt;  
	}  
	
	/**
	 * max of n is 4 ,that is we can convert byte(1-4) to integer
	 * @param b
	 * @param start
	 * @param n
	 * @return
	 */
	public int readIntFromByte(byte[] b,int start,int n){
		int res = 0;
		res = b[start];
		for(int i =1;i<n;i++){
			res = (res<<8)+b[start+i];
		}
		return res ;
	}
	
	public long readLongFromByte(byte[] b,int start){
		long res = 0;
		res = b[start];
		for(int i =1;i<8;i++){
			res = (res<<8)+b[start+i];
		}
		return res ;
	}
	
	public String readStrFromByte(byte[] b ,int start,int n){

		byte[] tem = new byte[n];
		String temstr = "";
		for(int i =0;i<n;i++){
			tem[i] = b[start+i];
			temstr+=(char)b[start+i];
		}
		return temstr;		
	}
	
	public String readStrFromByte(byte[] b ,int start){
		String temstr = "";
		int n = (int)b[start];
		byte[] tem = new byte[n];
		for(int i =0;i<n;i++){
			tem[i] = b[start+i+1];
			temstr +=(char)b[start+i+1];
		}	
		return temstr;		
	}
	
	public String readStrFromByte(byte[] b ,int start,boolean check){
		String temstr = "";
		int n = readIntFromByte(b,0,2);
		byte[] tem = new byte[n];
		for(int i =0;i<n;i++){
			tem[i] = b[start+i+1];
			temstr +=(char)b[start+i+1];
		}	
		return temstr;		
	}
	
	
	
	public byte[] addByte(byte[] b ,int add,int start,int len){
		
		if(len == 1){
			byte temp = (byte)add;
			b[start] = temp;
		}else if(len == 2){
			byte[] temp = new byte[2];
			temp[0] = (byte)(add & 0xff);
			temp[1] = (byte) ((0xff00 & add) >> 8);
			b[start] = temp[1];
			b[start+1] = temp[0];
		}else if(len == 3){
			byte[] temp = new byte[3];
		    temp[0] = (byte) (0xff & add);  
		    temp[1] = (byte) ((0xff00 & add) >> 8);  
		    temp[2] = (byte) ((0xff0000 & add) >> 16);
		    b[start] = temp[2] ;
		    b[start+1] = temp[1];
		    b[start+2] = temp[0];
		}else if(len == 4){
			byte[] temp = new byte[4];
			temp = FileRW.int2byte(add);
			b[start] = temp[0] ;
			b[start+1] = temp[1];
			b[start+2] = temp[2];
			b[start+3] = temp[3];			
		}	
		return b;
	}
	
	public byte[] addByte(byte[] b ,String add,int start){
	
		int lenstr = add.length();
		for(int i =0;i<lenstr;i++){
			b[i+start] = (byte)add.charAt(i);
		}		
		return b;
	}
	
	
	 public static DataInputStream fileOpenIn(String fileName){
		 DataInputStream in = null;
		 try {
			in = new DataInputStream(
					  new BufferedInputStream(
					  new FileInputStream(fileName)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		 return in;
	 }
	 
	 public long getFileLen(String fileName){
		 File fr = new File(fileName);
		 return fr.length();
	 }
	 
	 public static DataOutputStream fileOpenOut(String fileName) throws FileNotFoundException{
		 DataOutputStream out=new DataOutputStream(  
                 new BufferedOutputStream(  
                 new FileOutputStream(fileName)));  
		 return out;
	 }
	
	 public void reset(String fileName)  
	    {  	       
		 DataOutputStream out = null; 
	        try  
	        {  
	            out = fileOpenOut(fileName);
	            for(long i=0;i<30*1024*1024/4;i++){
	            	out.writeInt(0);
	            }
	            out.close();  
	        } catch (Exception e)  
	        {  
	            e.printStackTrace();  
	        }  
	    }  
	 	 
	 public int readInt(String fileName,int n) throws IOException{		 
		 int res = 0;
		 RandomAccessFile fr = new RandomAccessFile(fileName,"r");
		 try {
			fr.seek(n);
			res = fr.readInt();
			fr.close();
		 } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	 }


	 
	 public String readString(String fileName,int n,int m) throws FileNotFoundException{
		 String res = "";
		 RandomAccessFile fr = new RandomAccessFile(fileName,"r");
		 try {
			fr.seek(n);
			for(int i =1;i!=m+1;i++){
				res+=fr.readChar();
			}
			fr.close();
		 } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	 }

	 public void writeNum(String fileName,int n,int m) throws FileNotFoundException{
		 RandomAccessFile dos = new RandomAccessFile(fileName,"rw");
		 try {
			dos.seek(n);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 try {
			dos.writeInt(m);
			dos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	 
	 }
	 
	 public void writeNum(String fileName,int n,String res) throws FileNotFoundException{
		 RandomAccessFile fw = new RandomAccessFile(fileName,"rw");
		 try {
			fw.seek(n);
			fw.writeChars(res);
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return ;		 
	 }
	 
	 public void writeNum(String fileName,int n,double res) throws FileNotFoundException{
		 RandomAccessFile fw = new RandomAccessFile(fileName,"rw");
		 try {
			fw.seek(n);
			fw.writeDouble(res);
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return ;		 
	 }
	 
	 public byte[] readBytes(String fileName,int n,int m) throws FileNotFoundException{
		 byte[] res = new byte[m] ;
		 int i;
		 RandomAccessFile fr = new RandomAccessFile(fileName,"rw");
		 try {
			fr.seek(n);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 for(i=0;i!=m;i++){
			 try {
				res[i]=fr.readByte();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		 }
//		 fr.close();
		 return res;
	 }


	 public void writeBytes(String fileName,int n,byte[] res) throws FileNotFoundException{
		 RandomAccessFile fr = new RandomAccessFile(fileName,"rw");
		 try {
			fr.seek(n);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			 try {
				fr.write(res);
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	 
	 public void appendBytes(String fileName,byte[] b){
		 FileOutputStream fo;
		try {
			fo = new FileOutputStream(new File(fileName),true);
			BufferedOutputStream bfo = new BufferedOutputStream(fo);
			bfo.write(b);
			bfo.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	 } 
	 
}
